package org.NooLab.itexx.retrieval;

import java.util.ArrayList;

import org.NooLab.itexx.ITexxWindowIntf;
import org.NooLab.itexx.app.controller.content.SearchEngine;
import org.NooLab.utilities.strings.StringsUtil;




public class RequestUrlManager {

	ITexxWindowIntf mainApp ; 
	RetrievalEngine retrievalEngine ;
	// ========================================================================
	public RequestUrlManager( ITexxWindowIntf app, 
							  RetrievalEngine rEngine) {
		//
		mainApp = app;
		retrievalEngine = rEngine;
	}
	
	
	// ========================================================================
	public String prepareSearchEngineUrl(String searchStr, String engine, int startindex) {
		
		// ArrayList<String> rqStrs= new ArrayList<String>();
		String rqStr="";
		
		if (engine.toLowerCase().contains("google")){
			// we have multiples of 10 by default
			rqStr = prepareGoogleRequestString(searchStr,startindex);
		}

		
		// http://www.bing.com/search?q=putnam+progam&go=&qs=ds&form=QBLH&filt=all
		
		// http://www.bing.com/search?q=putnam+progam&go=&qs=ds&filt=all&first=81
		
		
		return rqStr;
	}


	/**
	 * prepares a basic request string -> put it to an object !!! (because there are lots of options
	 * @param searchstr
	 * @return
	 */
	private String prepareGoogleRequestString(String searchStr, int startIndex) {
		// 
		String searchstr,rqStr = ""; // read the template from config file
		
		searchstr = prepareSearchString(searchStr,startIndex);
		
		String startIndicator="";
		if (startIndex>1){
			startIndicator="&start="+startIndex;
		}
		
		rqStr = "http://www.google.com/search?num=100&hl=&newwindow=0"+startIndicator+"&sa=N&q="+searchstr ;// processing+p5&oq=processing+p5
		
		return rqStr;
	}

	private String prepareSearchString(String searchstr, int startindex) {

		searchstr = searchstr.trim();
		searchstr = StringsUtil.replaceall(searchstr, "  ", " ");
		searchstr = StringsUtil.replaceall(searchstr, " ", "+");
		searchstr = StringsUtil.replaceall(searchstr, " ", "");
		
		return searchstr ;
	}


	public String prepareSearchEngineUrl(String searchStr, SearchEngine se, int startindex) {
		// 
		String searchstr , query, rqStr = ""; 
		
		query = se.getQueryUrl() ;
		
		// this contains markers [?marker?]
		// available markers are lang, site and q
		// basicqueryurl=/search?q=[?q?]num=10&hl=[?lang?]&newwindow=0&source=hp|||
		
		query = query.replace("[?lang?]", "en"); 
		// this is just for internal standardization, page will use hints in english language
		
		if (searchStr.contains("site:")){
			
			searchstr = searchStr.replace(":", "%3A");
			searchStr = searchstr.replace("/", "%2F");
			//searchStr = searchstr.replace(" ", "%20"); only in site parameter
			// site%3Ahttp%3A%2F%2Fwww.academia.edu%2FDocuments%2Fin%2F
		}
		// the site constraint is part of the query string
		searchstr = prepareSearchString(searchStr,startindex);
		
		query = query.replace("[?q?]", searchstr); 
		
		query = query.replace("[?n?]", ""+startindex);
		
		rqStr = se.getAddress() + query;
		
		return rqStr ;
	}
	
	
}
